﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Class;

namespace WebApplication2
{
    public partial class HDMonitoringMeds : System.Web.UI.Page
    {
        int language = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            language = (int)Session["Language"];
            if (!Page.IsPostBack)
            {
                Deserialize_json_Object_and_Bind_GridView();
                changeIndexToWords();
                
            }
            if (GridView1.Rows.Count != 0)
            {
                populateFooter();
            }
        }

        public void Deserialize_json_Object_and_Bind_GridView()
        {
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRun/GetHDRunInCentreByHDTreatmentID?HDTreatmentID=1");
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"FMTXHDRun\":" + json + "}";

            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<MonitoringKeyWrapper>(json);
            GridView1.DataSource = keysWrapper.fmtxhdrun;
            GridView1.ShowFooter = true;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex == GridView1.EditIndex)
            {
                WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                req.ContentType = "application/json";
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                StreamReader re = new StreamReader(stream);
                String json = re.ReadToEnd();
                json = "{\"FMTXHDRunLabel\":" + json + "}";

                JavaScriptSerializer ser = new JavaScriptSerializer();
                var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                foreach (var item in KeyWapper.fmtxhdrunlabel)
                {
                    DropDownList ddlMedication_fluid = e.Row.FindControl("ddlMedication_fluid") as DropDownList;
                    if (item.Medication_FluidsID != 0)
                    {
                        ddlMedication_fluid.Items.Add(item.Medication_Fluids_Language.ToString());
                    }

                    DropDownList ddlStrength = e.Row.FindControl("ddlStrength") as DropDownList;
                    if (item.StrengthID != 0)
                    {
                        ddlStrength.Items.Add(item.Strength.ToString());
                    }

                    DropDownList ddlRoute = e.Row.FindControl("ddlRoute") as DropDownList;
                    if (item.RouteID != 0)
                    {
                        ddlRoute.Items.Add(item.Route_Language.ToString());
                    }

                    DropDownList ddlDose_Ordered = e.Row.FindControl("ddlDose_Ordered") as DropDownList;
                    if (item.DosageID != 0)
                    {
                        ddlDose_Ordered.Items.Add(item.Dosage_Language.ToString());
                    }

                    DropDownList ddlMed_Type = e.Row.FindControl("ddlMed_Type") as DropDownList;
                    if (item.MedTypeID != 0)
                    {
                        ddlMed_Type.Items.Add(item.Med_Type_Language.ToString());
                    }

                    DropDownList ddlFrequency = e.Row.FindControl("ddlFrequency") as DropDownList;
                    if (item.FrequencyID != 0)
                    {
                        ddlFrequency.Items.Add(item.Frequency_Language.ToString());
                    }

                    DropDownList ddlDose_Given = e.Row.FindControl("ddlDose_Given") as DropDownList;
                    if (item.DosageID != 0)
                    {
                        ddlDose_Given.Items.Add(item.Dosage_Language.ToString());
                    }

                    DropDownList ddlWitness_By = e.Row.FindControl("ddlWitness_By") as DropDownList;
                    if (item.User_Id != 0)
                    {
                        ddlWitness_By.Items.Add(item.Initials.ToString());
                    }

                    DropDownList ddlSignoff_by1 = e.Row.FindControl("ddlSignoff_by1") as DropDownList;
                    if (item.User_Id != 0)
                    {
                        ddlSignoff_by1.Items.Add(item.Initials.ToString());
                    }
                }
            }


            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (e.Row.Cells.Count > 0)
                {
                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"FMTXHDRunLabel\":" + json + "}";

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                    foreach (var item in KeyWapper.fmtxhdrunlabel)
                    {
                        if (item.FormLabelCode == "TxSc3-GpMon")
                        {
                            lblMonitoring.Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "TxSc3-GpICM")
                        {
                            lblInCentreMeds.Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Atime")
                        {
                            e.Row.Cells[1].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Meds")
                        {
                            e.Row.Cells[3].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Strength")
                        {
                            e.Row.Cells[4].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Route")
                        {
                            e.Row.Cells[5].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-ODose")
                        {
                            e.Row.Cells[6].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Mtype")
                        {
                            e.Row.Cells[7].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Mfreq")
                        {
                            e.Row.Cells[8].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-GDose")
                        {
                            e.Row.Cells[9].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Witness")
                        {
                            e.Row.Cells[10].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Signoff")
                        {
                            e.Row.Cells[11].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc4-Com")
                        {
                            e.Row.Cells[12].Text = item.Language.ToString();
                        }
                    }
                }
            }
        }

        public void changeIndexToWords()
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                Label Medication_fluid = (Label)row.FindControl("lblMedication_fluid");
                Label Strength = (Label)row.FindControl("lblStrength");
                Label Route = (Label)row.FindControl("lblRoute");
                Label Dose_Ordered = (Label)row.FindControl("lblDose_Ordered");
                Label Med_Type = (Label)row.FindControl("lblMed_Type");
                Label Frequency = (Label)row.FindControl("lblFrequency");
                Label Dose_Given = (Label)row.FindControl("lblDose_Given");
                Label Witness_By = (Label)row.FindControl("lblWitness_By");
                Label Signoff_by = (Label)row.FindControl("lblSignoff_by");

                string Medication_fluidIndex = Medication_fluid.Text;
                string StrengthIndex = Strength.Text;
                string RouteIndex = Route.Text;
                string Dose_OrderedIndex = Dose_Ordered.Text;
                string Med_TypeIndex = Med_Type.Text;
                string FrequencyIndex = Frequency.Text;
                string Dose_GivenIndex = Dose_Given.Text;
                string Witness_ByIndex = Witness_By.Text;
                string Signoff_byIndex = Signoff_by.Text;

                WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                req.ContentType = "application/json";
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                StreamReader re = new StreamReader(stream);
                String json = re.ReadToEnd();
                json = "{\"FMTXHDRunLabel\":" + json + "}";

                JavaScriptSerializer ser = new JavaScriptSerializer();
                var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                foreach (var item in KeyWapper.fmtxhdrunlabel)
                {
                    if (item.Medication_FluidsID == Convert.ToInt32(Medication_fluidIndex))
                    {
                        GridView1.Rows[i].Cells[3].Text = item.Medication_Fluids_Language;
                    }

                    if (item.StrengthID == Convert.ToInt32(StrengthIndex))
                    {
                        GridView1.Rows[i].Cells[4].Text = item.Strength;
                    }

                    if (item.RouteID == Convert.ToInt32(RouteIndex))
                    {
                        GridView1.Rows[i].Cells[5].Text = item.Route_Language;
                    }

                    if (item.DosageID == Convert.ToInt32(Dose_OrderedIndex))
                    {
                        GridView1.Rows[i].Cells[6].Text = item.Dosage_Language;
                    }

                    if (item.MedTypeID == Convert.ToInt32(Med_TypeIndex))
                    {
                        GridView1.Rows[i].Cells[7].Text = item.Med_Type_Language;
                    }

                    if (item.FrequencyID == Convert.ToInt32(FrequencyIndex))
                    {
                        GridView1.Rows[i].Cells[8].Text = item.Frequency_Language;
                    }

                    if (item.DosageID == Convert.ToInt32(Dose_OrderedIndex))
                    {
                        GridView1.Rows[i].Cells[9].Text = item.Dosage_Language;
                    }

                    if (item.User_Id == Convert.ToInt32(Witness_ByIndex))
                    {
                        GridView1.Rows[i].Cells[10].Text = item.Initials;
                    }

                    if (item.User_Id == Convert.ToInt32(Signoff_byIndex))
                    {
                        GridView1.Rows[i].Cells[11].Text = item.Initials;
                    }
                }
            }
        }

        public void populateFooter()
        {
            DropDownList ddlMedication_fluid1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlMedication_fluid1"));
            DropDownList ddlStrength1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlStrength1"));
            DropDownList ddlRoute1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlRoute1"));
            DropDownList ddlDose_Ordered1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlDose_Ordered1"));
            DropDownList ddlMed_Type1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlMed_Type1"));
            DropDownList ddlFrequency1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlFrequency1"));
            DropDownList ddlDose_Given1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlDose_Given1"));
            DropDownList ddlWitness_By1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlWitness_By1"));
            DropDownList ddlSignoff_by1 = ((DropDownList)GridView1.FooterRow.FindControl("ddlSignoff_by1"));

            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"FMTXHDRunLabel\":" + json + "}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

            foreach (var item in KeyWapper.fmtxhdrunlabel)
            {
                if (item.Medication_FluidsID != 0)
                {
                    ddlMedication_fluid1.Items.Add(item.Medication_Fluids_Language.ToString());
                }

                if (item.StrengthID != 0)
                {
                    ddlStrength1.Items.Add(item.Strength.ToString());
                }

                if (item.RouteID != 0)
                {
                    ddlRoute1.Items.Add(item.Route_Language.ToString());
                }

                if (item.DosageID != 0)
                {
                    ddlDose_Ordered1.Items.Add(item.Dosage_Language.ToString());
                }

                if (item.MedTypeID != 0)
                {
                    ddlMed_Type1.Items.Add(item.Med_Type_Language.ToString());
                }

                if (item.FrequencyID != 0)
                {
                    ddlFrequency1.Items.Add(item.Frequency_Language.ToString());
                }

                if (item.DosageID != 0)
                {
                    ddlDose_Given1.Items.Add(item.Dosage_Language.ToString());
                }

                if (item.User_Id != 0)
                {
                    if (item.Initials.ToString() != "")
                    {
                        ddlWitness_By1.Items.Add(item.Initials.ToString());
                    }
                }

                if (item.User_Id != 0)
                {
                    if (item.Initials.ToString() != "")
                    {
                        ddlSignoff_by1.Items.Add(item.Initials.ToString());
                    }
                }
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Deserialize_json_Object_and_Bind_GridView();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Deserialize_json_Object_and_Bind_GridView();
            changeIndexToWords();
            populateFooter();
        }

    }
}